package com.tmc.service.baseinfo;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.dao.impl.DeptDaoImpl;
import com.tmc.dao.impl.UserDaoImpl;
import com.tmc.entity.common.EthDept;
import com.tmc.entity.common.EthUser;
import com.tmc.util.ObjectUtil;

@Service
@Transactional
public class DeptServiceImpl {
	
	@Autowired
	private DeptDaoImpl deptDao;
	
	@Autowired
	private UserDaoImpl userDao;
	
	private Log logger = LogFactory.getLog(this.getClass());

	public Pagination<EthDept> findByCon(ContextInfo context, EthDept cond, Integer pageIndex, Integer pageSize) {
		Pagination<EthDept> result = deptDao.findByCon(context, cond, pageIndex, pageSize);
		this.setExtInfo(context, result);
		return result;
	}
	
	private void setExtInfo(ContextInfo context, Pagination<EthDept> result) {
		if (result == null || result.getItems() == null) {
			return;
		}
		for (EthDept dept : result.getItems()) {
			//dept.setManager(this.getDeptManager(context, dept.getDeptId()));
			this.setExtInfo(context, dept);
		}
	}
	
	private void setExtInfo(ContextInfo context, EthDept result){
		if(result == null){
			return;
		}
		result.setManager(this.getDeptManager(context, result.getDeptId()));
	}
	
	private String getDeptManager(ContextInfo context, Long deptId) {
		List<EthUser> users = userDao.findDeptManager(context, deptId);
		String[] strs = new String[users.size()];
		int i = 0;
		for (EthUser user : users) {
			strs[i++] = user.getUserName();
		}
		return null;
		//return String.join(",", strs);
	}
	
	public CommonResult save(ContextInfo context, EthDept vendor) {
		CommonResult check = this.check(context, vendor);
		if(!check.isSuccess()){
			return check;
		}
		if(vendor.getDeptId() == null){
			//淇濆瓨
			ObjectUtil.setBaseInfo(vendor, context);
			deptDao.insert(vendor);
		} else {
			//缂栬緫
			try {
				EthDept dbDept = deptDao.getById(vendor.getDeptId());
				if(dbDept != null){
					ObjectUtil.copyProperties(vendor, dbDept,
							new String[] { "createdBy", "creationDate", "deptId", "enabledFlag" });
				}
			} catch (Exception e) {
				logger.error("鎷疯礉瀵硅薄閿欒锛�", e);
				return CommonResult.makeFail("鎷疯礉瀵硅薄閿欒锛�");
			}
		}
		return CommonResult.makeSuccess();
	}
	
	public EthDept getByName(ContextInfo context, String deptName){
		if (ObjectUtil.noText(deptName)) {
			return null;
		}
		return deptDao.getByName(context, deptName);
	}
	
	public EthDept getByCode(ContextInfo context, String code) {
		if (ObjectUtil.noText(code)) {
			return null;
		}
		return deptDao.getByCode(context, code);
	}
	
	public EthDept getById(ContextInfo context, Long id) {
		if (id == null) {
			return null;
		}
		return deptDao.getById(id);
	}

	private CommonResult check(ContextInfo context, EthDept vendor){
		if(ObjectUtil.noText(vendor.getDeptName())){
			CommonResult.makeFail("閮ㄩ棬鍚嶇О涓嶈兘涓虹┖锛�");
		}
		EthDept dbDept = deptDao.getByName(context, vendor.getDeptName());
		if(dbDept != null && !dbDept.getDeptId().equals(vendor.getDeptId())){
			return CommonResult.makeFail("閮ㄩ棬鍚嶇О宸插瓨鍦紒");
		}
		return CommonResult.makeSuccess();
		
	}

	public CommonResult delete(ContextInfo context, Long deptId) {
		EthDept dbDept = deptDao.getById(deptId);
		if(dbDept != null){
			ObjectUtil.updateBaseInfo(dbDept, context);
			dbDept.setEnabledFlag("N");
			deptDao.update(dbDept);
		} else {
			CommonResult.makeFail("鏁版嵁涓嶅瓨鍦紒");
		}
		return CommonResult.makeSuccess("鍒犻櫎鎴愬姛锛�");
	}

	public List<EthDept> searchDept(String query) {
		List<EthDept> list = new ArrayList<EthDept>();
		List<EthDept> dbList = null;
		if(ObjectUtil.noText(query)){
			Pagination<EthDept> result = deptDao.findByCon(new ContextInfo(), new EthDept(), 1, 10);
			dbList = result.getItems();
		} else {
			dbList = deptDao.findListByName(query);
		}
		for (EthDept dept : dbList) {
			list.add(dept);
			if (list.size() >= 5) {
				break;
			}
		}		 
		return list;
	}
	
}
